package cn.gok.oa.information.controller;


import cn.gok.oa.information.dto.InformationDto;
import cn.gok.oa.information.entity.TbInformation;
import cn.gok.oa.information.service.impl.TbCommentServiceImpl;
import cn.gok.oa.information.service.impl.TbInformationServiceImpl;
import cn.gok.oa.information.vo.CommentVo;
import cn.gok.oa.information.vo.InformationVo;
import cn.gok.oa.log.LogAnnotation;
import cn.gok.oa.log.entity.Log;
import cn.gok.oa.util.ResultData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/information")
public class TbInformationController {

    @Autowired
    TbInformationServiceImpl informationService;
    @Autowired
    TbCommentServiceImpl commentService;

    @RequestMapping(value = "/add")
    @LogAnnotation("新增资讯")
    @ResponseBody
    public ResultData add(@RequestBody TbInformation tbInformation){
        if(informationService.save(tbInformation))
            return ResultData.success();
        else
            return ResultData.fail();
    }

    @RequestMapping(value = "/info" )
    @LogAnnotation("获取企业资讯列表")
    @ResponseBody
    public ResultData info( InformationDto informationDto){
        Page<InformationVo> page = new Page<>(informationDto.getPage(),informationDto.getLimit());
        IPage<InformationVo> iPage = informationService.selectCommentList(page,informationDto.getSearch());
        for (InformationVo inf : iPage.getRecords()) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("information_id",inf.getId());
            inf.setComment(commentService.count(queryWrapper));
        }
        return ResultData.page(iPage.getRecords(),iPage.getTotal());
    }

    @RequestMapping(value = "/delete" )
    @LogAnnotation("删除企业资讯byID")
    @ResponseBody
    public ResultData delete(@Param("id") Integer id){
        if (informationService.removeById(id))
            return ResultData.success();
        else
            return ResultData.fail();
    }
    @RequestMapping(value = "/edit")
    @LogAnnotation("编辑企业资讯byID")
    public ResultData edit(@Param("id") Integer id){
        TbInformation tbInformation;
        tbInformation = informationService.getById(id);
        if (tbInformation!=null)
            return ResultData.buildData(tbInformation);
        else
            return ResultData.fail();
    }

    @RequestMapping(value = "/one-info")
    @LogAnnotation("获取单个资讯详情")
    public ResultData getOneinfo(@Param("id") Integer id){
        return ResultData.buildData(informationService.getOneInfo(id));
    }

    @RequestMapping(value = "/update")
    @LogAnnotation("更新企业资讯")
    public ResultData update(@RequestBody TbInformation tbInformation){
        if(informationService.updateById(tbInformation))
            return ResultData.success();
        else
            return ResultData.fail();
    }

}
